package com.academic.system.service.impl;

import com.academic.common.core.domain.entity.SysAttendance;
import com.academic.common.core.domain.entity.SysCourse;
import com.academic.common.core.domain.entity.SysUsers;
import com.academic.common.core.page.TableDataInfo;
import com.academic.system.domain.rep.AttendanceRep;
import com.academic.system.domain.req.NewReq;
import com.academic.system.mapper.SysUsersMapper;
import com.academic.system.mapper.dao.SysAttendanceDao;
import com.academic.system.mapper.dao.SysCourseDao;
import com.academic.system.service.SysAttendanceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (SysAttendance)表服务实现类
 *
 * @author makejava
 * @since 2024-10-31 22:55:00
 */
@Service("sysAttendanceService")
public class SysAttendanceServiceImpl extends ServiceImpl<SysAttendanceDao, SysAttendance> implements SysAttendanceService {

    private final SysAttendanceDao attendanceDao;
    private final SysCourseDao sysCourseDao;
    private final SysUsersMapper sysUsersMapper;
    private final  SysBookServiceImpl sysBookService;

    public SysAttendanceServiceImpl(SysAttendanceDao attendanceDao, SysCourseDao sysCourseDao, SysUsersMapper sysUsersMapper, SysBookServiceImpl sysBookService) {
        this.attendanceDao = attendanceDao;
        this.sysCourseDao = sysCourseDao;
        this.sysUsersMapper = sysUsersMapper;
        this.sysBookService = sysBookService;
    }

    @Override
    public TableDataInfo getAttendance(NewReq newReq) {
        //  获取用户考勤历史
        PageHelper.startPage(newReq.getPageNum(), newReq.getPageSize());
        List<AttendanceRep> attendanceReps = new ArrayList<>();
        QueryWrapper<SysAttendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", newReq.getId());
        List<SysAttendance> sysAttendances = attendanceDao.selectList(queryWrapper);
        PageInfo<SysAttendance> pageInfo = new PageInfo<>(sysAttendances);
        List<Long> courseIds = sysAttendances.stream().map(SysAttendance::getCourseId)
                .collect(Collectors.toList());

        // 检查courseIds是否为空
        if (courseIds.isEmpty()) {
            return null; // 直接返回空列表
        }
        QueryWrapper<SysCourse> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("id", courseIds);
        List<SysCourse> sysCourse = sysCourseDao.selectList(queryWrapper1);

        QueryWrapper<SysUsers> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id", newReq.getId());
        SysUsers sysUsers = sysUsersMapper.selectOne(queryWrapper2);

        for (SysAttendance sysAttendance : sysAttendances) {
            AttendanceRep attendanceRep = new AttendanceRep();
            SysCourse sysCourse1 = sysCourse.stream()
                    .filter(course -> course.getId()
                            .equals(sysAttendance.getCourseId()))
                    .findFirst()
                    .get();
            attendanceRep.setCourseName(sysCourse1.getCourseName());
            attendanceRep.setClassName(sysAttendance.getClassroom());
            attendanceRep.setStudentName(sysUsers.getNickName());
            attendanceRep.setStatus(sysAttendance.getStatus());
            attendanceRep.setCourseTime(sysAttendance.getCourseTime());
            attendanceRep.setCreateTime(sysAttendance.getCreateTime());
            attendanceRep.setUserId(newReq.getId());
            attendanceRep.setTeacherPhone(sysCourse1.getTeacherPhone());
            attendanceReps.add(attendanceRep);

        }
        return sysBookService.getDataTable(attendanceReps,pageInfo.getTotal());
    }
}

